package com.web;

import com.dao.FoodDao;
import com.dao.Impl.FoodImpl;
import com.google.gson.Gson;
import com.pojo.Callfood;
import com.pojo.Cart;
import com.pojo.CartItem;
import com.pojo.Food;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CartSevlet extends HttpServlet {
    FoodDao foodDao = new FoodImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        resp.setContentType("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        if ("addtocart".equals(action)) {
            String tempid = req.getParameter("id");
            int id = Integer.parseInt(tempid);
            Food food = foodDao.queryBookById(id);
            CartItem cartItem = new CartItem(food.getId(), food.getName(), 1, food.getPrice(), food.getPrice(), food.getImgpath());
            Cart cart = (Cart) req.getSession().getAttribute("cart");     //1
            if (cart == null) {
                cart = new Cart();
                req.getSession().setAttribute("cart", cart);     //2       1和2相对应
            }
            cart.addItem(cartItem);
            Map<String, Object> resultmap = new HashMap<String, Object>();
            resultmap.put("lastname", cartItem.getName());
            Gson gson = new Gson();
            String resultmapJsonString = gson.toJson(resultmap);
            resp.getWriter().write(resultmapJsonString);
        } else if ("deleteItem".equals(action)) {
            String tempid = req.getParameter("id");
            int id = Integer.parseInt(tempid);
            Cart cart = (Cart) req.getSession().getAttribute("cart");
            if (cart != null) {
                cart.deleteItem(id);
                //将浏览器当前地址栏发给服务器 进行重定向跳转
                resp.sendRedirect(req.getHeader("Referer"));
            }
        } else if ("clearcart".equals(action)) {
            Cart cart = (Cart) req.getSession().getAttribute("cart");
            if(cart!=null){
                cart.clear();
            }
            resp.sendRedirect(req.getHeader("Referer"));
        } else if ("updatecnt".equals(action)) {
            //获取商品的id 值   并且字串串转成int
            String tempid = req.getParameter("id");
            int id = Integer.parseInt(tempid);
            ////获取商品要修改的数量  并且字串串转成int
            String tempcnt = req.getParameter("count");
            int count = Integer.parseInt(tempcnt);
            //获取用户的购物车
            Cart cart = (Cart) req.getSession().getAttribute("cart");
            if (cart != null) {
                cart.updatecnt(id, count);
                resp.sendRedirect(req.getHeader("Referer"));
            }
        } else if ("vxpay".equals(action)) {
            req.getRequestDispatcher("pages/user/vxpay.jsp").forward(req, resp);
        } else if ("call".equals(action)) {                 //补货
            String bookname = req.getParameter("bookname");
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = format.format(date);
            String adr = req.getSession().getAttribute("louhao") + "#" + req.getSession().getAttribute("sushehao");
            foodDao.callFood(new Callfood(time, adr, bookname));
        } else if ("jian".equals(action)) {
            String tempid = req.getParameter("id");
            int id = Integer.parseInt(tempid);
            String tempcnt = req.getParameter("count");
            int count = Integer.parseInt(tempcnt);
            if (count != 0) {
                count--;
                Cart cart = (Cart) req.getSession().getAttribute("cart");
                if (cart != null) {
                    cart.updatecnt(id, count);
                    if (count==0){
                        cart.deleteItem(id);
                    }
                    resp.sendRedirect(req.getHeader("Referer"));
                }
            }
        } else if ("jia".equals(action)) {
            String tempid = req.getParameter("id");
            int id = Integer.parseInt(tempid);
            String tempcnt = req.getParameter("count");
            int count = Integer.parseInt(tempcnt);
            count++;
            Cart cart = (Cart) req.getSession().getAttribute("cart");
            if (cart != null) {
                cart.updatecnt(id, count);
                resp.sendRedirect(req.getHeader("Referer"));
            }

        }
    }
}
